//
//  ViewController.m
//  ColorTest
//
//  Created by 徐凯 on 2017/6/13.
//  Copyright © 2017年 XK. All rights reserved.
//

#import "ViewController.h"
#import "UIColor+YYAdd.h"


#define RGBCOLOR(r,g,b)   [UIColor colorWithRed:(r/255.0) green:(g/255.0) blue:(b/255.0) alpha:1]

@interface ViewController ()

@property (nonatomic, strong) UIView *testView;
@property (nonatomic, strong) UIView *tempView1;
@property (nonatomic, strong) UIView *tempView2;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _testView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 300, 44)];
    [self.view addSubview:_testView];
    
    UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    [_testView addGestureRecognizer:recognizer];
    
    _tempView1 = [[UIView alloc] initWithFrame:CGRectMake(100, 220, 300, 44)];
    _tempView1.backgroundColor = [UIColor grayColor];
    [self.view addSubview:_tempView1];
    
    _tempView2 = [[UIView alloc] initWithFrame:CGRectMake(100, 340, 300, 44)];
    _tempView2.backgroundColor = [UIColor grayColor];
    [self.view addSubview:_tempView2];
    
    
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.colors = @[(__bridge id)[UIColor colorWithHexString:@"000000"].CGColor,
                             (__bridge id)[UIColor redColor].CGColor,
                             (__bridge id)[UIColor colorWithHexString:@"FFFFFF"].CGColor];
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1.0, 0);
    gradientLayer.frame = _testView.bounds;
    [_testView.layer addSublayer:gradientLayer];
    

    
}


-(void) handlePan:(UIPanGestureRecognizer *) pan
{
    CGPoint translatedPoint = [pan locationInView:_testView];
    
    CGFloat x = translatedPoint.x;
    if(x < 0)
    {
        x = 0;
    }
    
    if(x > _testView.frame.size.width)
    {
        x = _testView.frame.size.width;
    }
    
    CGFloat sp = x / _testView.frame.size.width;
    
    

    const CGFloat *wCS=CGColorGetComponents([UIColor colorWithHexString:@"000000"].CGColor);
    const CGFloat *bCS=CGColorGetComponents([UIColor redColor].CGColor);
    const CGFloat *rCS=CGColorGetComponents([UIColor colorWithHexString:@"FFFFFF"].CGColor);
    
    
    int Wr = wCS[0] * 255;
    int Wg = wCS[1] * 255;
    int Wb = wCS[2] * 255;
    
    int Br = bCS[0] * 255;
    int Bg = bCS[1] * 255;
    int Bb = bCS[2] * 255;
    
    int Rr = rCS[0] * 255;
    int Rg = rCS[1] * 255;
    int Rb = rCS[2] * 255;
    
    int Cr = Br - Rr;
    int Cg = Bg - Rg;
    int Cb = Bb - Rb;
    
    int Cr1 = Br - Wr;
    int Cg1 = Bg - Wg;
    int Cb1 = Bb - Wb;

    
    
    CGFloat sp1 = (sp - 0.5) / 0.5;
    int Tr = Br - Cr * sp1;
    int Tg = Bg - Cg * sp1;
    int Tb = Bb - Cb * sp1;
    
    CGFloat sp2 = sp / 0.5;
    int Tr1 = Wr + Cr1 * sp2;
    int Tg1 = Wg + Cg1 * sp2;
    int Tb1 = Wb + Cb1 * sp2;
    
     NSString *strST = @"000000";
    if(sp <= 0.5)
    {
        NSString *STr = [NSString stringWithFormat:@"%@",[self ToHex:Tr1]];
        NSString *STg = [NSString stringWithFormat:@"%@",[self ToHex:Tg1]];
        NSString *STb = [NSString stringWithFormat:@"%@",[self ToHex:Tb1]];
        strST =[NSString stringWithFormat:@"%@%@%@",STr,STg,STb];
    }
    else
    {
        NSString *STr = [NSString stringWithFormat:@"%@",[self ToHex:Tr]];
        NSString *STg = [NSString stringWithFormat:@"%@",[self ToHex:Tg]];
        NSString *STb = [NSString stringWithFormat:@"%@",[self ToHex:Tb]];
        strST =[NSString stringWithFormat:@"%@%@%@",STr,STg,STb];
    }
    
    _tempView1.backgroundColor = [UIColor colorWithHexString:strST];
    

}





//颜色转字符串
- (NSString *) changeUIColorToRGB:(UIColor *)color{
    
    
    const CGFloat *cs=CGColorGetComponents(color.CGColor);
    
    int r1 = cs[0]*255;
    int g1 = cs[1]*255;
    int b1 = cs[2]*255;
    
    NSString *r = [NSString stringWithFormat:@"%@",[self  ToHex:cs[0]*255]];
    NSString *g = [NSString stringWithFormat:@"%@",[self  ToHex:cs[1]*255]];
    NSString *b = [NSString stringWithFormat:@"%@",[self  ToHex:cs[2]*255]];
    
    return [NSString stringWithFormat:@"%@%@%@",r,g,b];
    
    
}


//十进制转十六进制
-(NSString *)ToHex:(int)tmpid
{
    NSString *endtmp=@"";
    NSString *nLetterValue;
    NSString *nStrat;
    int ttmpig=tmpid%16;
    int tmp=tmpid/16;
    switch (ttmpig)
    {
        case 10:
            nLetterValue =@"A";break;
        case 11:
            nLetterValue =@"B";break;
        case 12:
            nLetterValue =@"C";break;
        case 13:
            nLetterValue =@"D";break;
        case 14:
            nLetterValue =@"E";break;
        case 15:
            nLetterValue =@"F";break;
        default:nLetterValue=[[NSString alloc]initWithFormat:@"%i",ttmpig];
            
    }
    switch (tmp)
    {
        case 10:
            nStrat =@"A";break;
        case 11:
            nStrat =@"B";break;
        case 12:
            nStrat =@"C";break;
        case 13:
            nStrat =@"D";break;
        case 14:
            nStrat =@"E";break;
        case 15:
            nStrat =@"F";break;
        default:nStrat=[[NSString alloc]initWithFormat:@"%i",tmp];
            
    }
    endtmp=[[NSString alloc]initWithFormat:@"%@%@",nStrat,nLetterValue];
    return endtmp;
}


@end
